package com.hihiwjc.app.zzmh.adapters.home;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hihiwjc.app.zzmh.R;
import com.hihiwjc.app.zzmh.entities.home.Recommend;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * <br/>Created by hihiwjc on 2015/10/28 0028
 * <br/>Author:hihiwjc
 * <br/>Email:hihiwjc@live.com
 */

public class RecommendBannerPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<Recommend.BannersEntity> mDatas;
    private View.OnClickListener mListener;

    RecommendBannerPagerAdapter(@NonNull Context context, @NonNull List<Recommend.BannersEntity> datas, @NonNull View.OnClickListener listener) {
        mContext = context;
        mDatas = datas;
        mListener = listener;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    public int getRealCount() {
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return object == view;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int realPosition = position % mDatas.size();
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.item0_vp_lv_fragment_main_recommend_banners, container, false);
        ImageView view = (ImageView) rootView.findViewById(R.id.iv_item_vp_lv_fragment_main_recommend_banners);
        Recommend.BannersEntity bannersEntity = mDatas.get(realPosition);
        Picasso.with(mContext).load(bannersEntity.getTheme_info().getCover()).fit().placeholder(R.mipmap.ic_launcher).into(view);
        container.addView(rootView);
        view.setOnClickListener(mListener);
        view.setTag(bannersEntity);
        return rootView;
    }

    public Recommend.BannersEntity getItem(int position) {
        int realPosition = position % mDatas.size();
        return mDatas.get(realPosition);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
